from django.db import models
from isa.models import CUser



# Create your models here.


class Articles(models.Model):
    author = models.ForeignKey(CUser)

    title = models.CharField(max_length=200,default="Untitle")
    content = models.TextField(default="write here ...")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=b'created at')

    attach_file = models.FileField(upload_to="UsersAttachs",default=None)
    link = models.CharField(max_length=255,default=None)

    def __str__(self):
        return self.get_abstruct()

    def get_abstruct(self):
        if len(self.content) > 397:
            return self.content[:397]+"..."
        else:
            return self.content

    @staticmethod
    def get_by_tag(tag):
        if tag== "all":
            return Articles.objects.all()
        ars = Articles.objects.all()
        return [ar for ar in ars if tag in  [t.content for t in  ar.atags_set.all()] ]

class Commits(models.Model):
    article = models.ForeignKey(Articles)
    
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=b'created at')
    write_name = models.CharField(max_length=255,default="Anoninmous")
    content = models.TextField()



class ATags(models.Model):
    article = models.ForeignKey(Articles)
    content = models.CharField(max_length=100)
    

    @staticmethod
    def get_all_tags(user):
        ars = user.articles_set.all()
        tags = set()
        tags = [[ tags.add(tag.content) for tag in  ar.atags_set.all() ] for ar in ars]
        return tags
